package netty.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StringInitializer extends ChannelInitializer<SocketChannel> {
    public static final Logger LOG = LoggerFactory.getLogger(StringInitializer.class);

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // tail is ("\n") split decoder
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(10000, Delimiters.lineDelimiter()));

        // string decoder and encoder
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        // my Handler
        pipeline.addLast("handler", new StringHandler());
    }
}